package com.szxy.cart.controller;

import com.netflix.discovery.converters.Auto;
import com.szxy.cart.service.CookieCartService;
import com.szxy.cart.service.RedisCartService;
import com.szxy.utils.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther:zwer
 * @Date:2019/9/12 10:08
 * @Description:com.szxy.cart
 * @Version:1.0
 **/
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CookieCartService cookieCartService;
    @Autowired
    private RedisCartService redisCartService;

    /**
     * 增加商品到购物车
     * @param userId
     * @param itemId
     * @param num
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/addItem")
    public Result addItem(String userId, Long itemId
                        ,@RequestParam(defaultValue = "1")Integer num
                        , HttpServletRequest request
                        , HttpServletResponse response){
        try {
            if(StringUtils.isBlank(userId)){
                //  用户未登录状态
                return this.cookieCartService.addItem(itemId,num,request,response);
            }else{
                // 用户已经的状态
                return this.redisCartService.addItem(itemId,num,userId);
            }
            // TODO: 2019/9/12
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(500,"error");
    }

    /**
     * 展示购物车列表
     * @param userId
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/showCart")
    public Result showCart(String userId,HttpServletRequest request,HttpServletResponse response){
        try {
            if(StringUtils.isBlank(userId)){
                //用户未登录状态
                return this.cookieCartService.showCart(request,response);
            }else{
                //用户已登录状态
                return this.redisCartService.showCart(userId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(500,"error");
    }

    /**
     * 更新商品数量
     * @param num
     * @param userId
     * @param itemId
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/updateItemNum")
    public Result updateItemNum(Integer num,String userId,Long itemId
                        ,HttpServletRequest request,HttpServletResponse response){
        try {
            if(StringUtils.isBlank(userId)){
                //用户未登录状态
                return this.cookieCartService.updateItemNum(itemId,num,request,response);
            }else{
                //用户已登录状态
                return this.redisCartService.updateItemNum(itemId,num,userId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(500,"error");
    }

    /**
     * 从购物车中删除商品
     * @param userId
     * @param itemId
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/deleteItemFromCart")
    public Result deleteItemFromCart(
                    @RequestParam String userId
                    ,@RequestParam  Long itemId
                    ,HttpServletRequest request,HttpServletResponse response){
        try {
            if(StringUtils.isBlank(userId)){
                // 用户未登录状态
                return this.cookieCartService.deleteItemFromCart(itemId,request,response);
            }else{
                // 用户已登录状态
                return this.redisCartService.deleteItemFromCart(itemId, userId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(500,"error");
    }

    @PostMapping("/goSettlement")
    public Result goSettlement(String[] ids, String userId){
        try{
            return this.redisCartService.goSettlement(ids, userId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.build(500, "去结算失败");
    }

}
